<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>药店管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="/layui/css/layui.css" rel="stylesheet">
    <script src="/jquery/jquery-3.6.0.js"></script>
</head>

<body>
<div class="layui-layout layui-layout-admin">
    <div class="layui-header">
        <div class="layui-logo layui-hide-xs layui-bg-black">小药店管理系统</div>
        <!-- 头部区域（可配合layui 已有的水平导航） -->
        <ul class="layui-nav layui-layout-right">
            <li class="layui-nav-item layui-hide layui-show-sm-inline-block">
                <a>
                    <img src="//unpkg.com/outeres@0.0.10/img/layui/icon-v2.png" class="layui-nav-img">
                    欢迎, <span id="username">管理员</span>
                </a>
                <dl class="layui-nav-child">
                    <dd><a href="javascript:;" onclick="logout()">退出</a></dd>
                </dl>
            </li>
        </ul>
    </div>
    <div class="layui-side layui-bg-black">
        <div class="layui-side-scroll">
            <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
            <ul class="layui-nav layui-nav-tree" lay-filter="test">
                <li class="layui-nav-item layui-nav-itemed">
                    <a class="" href="javascript:;">采购管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="supplier.html">供应商信息管理</a></dd>
                        <dd><a href="drug.html">药品信息管理</a></dd>
                        <dd><a href="../procurement/add.html">采购单添加</a></dd>
                        <dd><a href="../procurement/list.html">采购单管理</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item">
                    <a href="javascript:;">销售管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="../sales/add_xiaoshoudan.html">销售单添加</a></dd>
                        <dd><a href="../sales/xiaoshoumingxi.html">销售单管理</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item">
                    <a href="javascript:;">药店统计管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="../statistics/instock.html">入库统计</a></dd>
                        <dd><a href="../statistics/sales.html">销售统计</a></dd>
                        <dd><a href="../statistics/stock.html">库存查询</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item">
                    <a href="javascript:;">人事管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="../yuangong/add_yuangong.html">员工添加</a></dd>
                        <dd><a href="../yuangong/yuangong.html">员工管理</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item">
                    <a href="javascript:;">打印管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="../print/ruku.html">入库单</a></dd>
                        <dd><a href="../print/chuku.html">出库单</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item">
                    <a href="javascript:;">系统管理</a>
                    <dl class="layui-nav-child">
                        <dd><a href="../user/xiuGaiMiMa.html">修改密码</a></dd>
                    </dl>
                </li>
            </ul>
        </div>
    </div>
    <div class="layui-body">
        <!-- 内容主体区域 -->
        <div class="layui-form layui-form-pane" style="margin: 15px;">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">供应商名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="searchName" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn" lay-submit lay-filter="searchSubmit">
                        <i class="layui-icon layui-icon-search"></i> 搜索
                    </button>
                    <button class="layui-btn layui-btn-normal" id="addSupplier">
                        <i class="layui-icon layui-icon-add-1"></i> 添加供应商
                    </button>
                </div>
            </div>
        </div>

        <!-- 数据表格 -->
        <table id="supplierTable" lay-filter="supplierTable"></table>

        <!-- 表格工具栏模板 -->
        <script type="text/html" id="tableToolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="edit">编辑</button>
                <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</button>
            </div>
        </script>

        <!-- 添加/编辑供应商表单模板 -->
        <script type="text/html" id="supplierForm">
            <form class="layui-form" style="padding: 20px;" lay-filter="supplierFormFilter">
                <input type="hidden" name="id">
                <div class="layui-form-item">
                    <label class="layui-form-label">单位名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="mingcheng" required lay-verify="required" placeholder="请输入单位名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">地址</label>
                    <div class="layui-input-block">
                        <input type="text" name="dizhi" required lay-verify="required" placeholder="请输入地址" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">联系人</label>
                    <div class="layui-input-block">
                        <input type="text" name="lianxiren" required lay-verify="required" placeholder="请输入联系人" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">联系电话</label>
                    <div class="layui-input-block">
                        <input type="text" name="dianhua" required lay-verify="required" placeholder="请输入联系电话" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">邮编</label>
                    <div class="layui-input-block">
                        <input type="text" name="youbian" required lay-verify="required" placeholder="请输入邮编" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">传真</label>
                    <div class="layui-input-block">
                        <input type="text" name="chuanzhen" required lay-verify="required" placeholder="请输入传真" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">电子邮箱</label>
                    <div class="layui-input-block">
                        <input type="text" name="youxiang" required lay-verify="required" placeholder="请输入电子邮箱" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">合作状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="1" title="合作中" checked>
                        <input type="radio" name="status" value="0" title="未合作">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="supplierSubmit">提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </script>
    </div>
</div>
</body>

<script src="/layui/layui.js"></script>
<script>
    function logout() {
        $.ajax({
            url: '/user/logout',
            type: 'GET',
            success: function(res) {
                if(res.code === 0) {
                    window.location.href = '/login/login.html';
                } else {
                    layer.msg(res.msg || '退出失败', {icon: 2});
                }
            },
            error: function() {
                layer.msg('服务器错误', {icon: 2});
            }
        });
    }

    layui.use(['table', 'form', 'layer'], function(){
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;

        // 初始化表格
        table.render({
            elem: '#supplierTable',
            url: '/supplierInformation/selectByLimit',
            toolbar: true,
            defaultToolbar: ['filter', 'exports', 'print'],
            request: {
                pageName: 'page',
                limitName: 'limit'
            },
            response: {
                statusName: 'code',
                statusCode: 0,
                msgName: 'msg',
                countName: 'count',
                dataName: 'data'
            },
            cols: [[
                {field: 'id', title: 'ID', sort: true},
                {field: 'mingcheng', title: '单位名称'},
                {field: 'dizhi', title: '地址'},
                {field: 'lianxiren', title: '联系人'},
                {field: 'dianhua', title: '联系电话'},
                {field: 'youbian', title: '邮编'},
                {field: 'chuanzhen', title: '传真'},
                {field: 'youxiang', title: '电子邮箱'},
                {field: 'status', title: '状态', templet: function(d){
                        return d.status == 1 ? '合作中' : '未合作';
                    }},
                {title: '操作', toolbar: '#tableToolbar', width: 150}
            ]],
            page: true,
            loading: true,
            error: function(res, msg){
                console.log(res);
                layer.msg('数据加载失败：' + msg);
            },
            parseData: function(res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.count,
                    "data": res.data
                };
            }
        });

        // 添加供应商按钮点击事件
        $('#addSupplier').on('click', function(){
            layer.open({
                type: 1,
                title: '添加供应商',
                area: ['500px', '600px'],
                content: $('#supplierForm').html()
            });
            form.render();
        });

        // 监听工具条事件
        table.on('tool(supplierTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                layer.open({
                    type: 1,
                    title: '编辑供应商',
                    area: ['500px', '600px'],
                    content: $('#supplierForm').html(),
                    success: function(layero, index){
                        form.val('supplierFormFilter', data);
                        form.render();
                    }
                });
            } else if(obj.event === 'del'){
                layer.confirm('确定删除该供应商？', function(index){
                    $.ajax({
                        url: '/supplierInformation/deleteById?id=' + data.id,
                        type: 'DELETE',
                        success: function(res){
                            if(res.code === 200){
                                layer.msg('删除成功');
                                obj.del();
                            } else {
                                layer.msg('删除失败：' + res.msg);
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });

        // 监听表单提交
        form.on('submit(supplierSubmit)', function(data){
            // 表单数据验证
            if (!data.field.mingcheng) {
                layer.msg('请输入单位名称');
                return false;
            }
            if (!data.field.dizhi) {
                layer.msg('请输入地址');
                return false;
            }
            if (!data.field.lianxiren) {
                layer.msg('请输入联系人');
                return false;
            }

            var url = data.field.id ? '/supplierInformation/updateById' : '/supplierInformation/insert';
            console.log('提交的数据:', data.field);  // 添加调试日志
            $.ajax({
                url: url,
                type: data.field.id ? 'PUT' : 'POST',
                contentType: 'application/json',
                data: JSON.stringify(data.field),
                success: function(res){
                    if(res.code === 200){
                        layer.closeAll();
                        layer.msg('操作成功');
                        table.reload('supplierTable');
                    } else {
                        layer.msg('操作失败：' + res.msg);
                    }
                },
                error: function(xhr, status, error) {
                    console.log('提交错误:', error);
                    console.log('响应文本:', xhr.responseText);
                    layer.msg('提交失败：' + error);
                }
            });
            return false;
        });

        // 监听搜索提交
        form.on('submit(searchSubmit)', function(data){
            table.reload('supplierTable', {
                where: {
                    mingcheng: data.field.searchName
                },
                page: {
                    curr: 1
                }
            });
            return false;
        });

        // 自定义验证规则
        form.verify({
            // 修改后的验证规则
        });
    });
</script>
</html>